<?php

namespace App\Models;

use Illuminate\Support\Str;
use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    const TYPE_NORMAL = 'normal';
    const TYPE_CROWDFUNDING = 'crowdfunding';

    public static $typeMap = [
        self::TYPE_NORMAL => '普通商品',
        self::TYPE_CROWDFUNDING => '众筹商品'
    ];
    protected $fillable = [
        'title',
        'description',
        'image',
        'on_sale',
        'rating',
        'sold_count',
        'review_count',
        'price',
        'type'
    ];

    protected $casts = [
        'on_sale' => 'boolean',
    ];
    // 与商品SKU 关联
    public function skus() {
        return $this->hasMany(ProductSku::class);
    }

    public function getImageUrlAttribute() {
        // 如果image 字段本身就已经是完整的url 就直接返回
        if(Str::startsWith($this->attributes['image'],['http://','https://'])) {
            return $this->attributes['image'];
        }
        return \Storage::disk('public')->url($this->attributes['image']);
    }

    public function category() {
        return $this->belongsTo(Category::class);
    }

    public function crowdfunding() {
        return $this->hasOne(CrowdfundingProduct::class);
    }
}
